package com.virtualmall.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
//import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.virtualmall.bean.Account;
import com.virtualmall.bean.ShoppingCart;
import com.virtualmall.common.Constants;
import com.virtualmall.common.Page;
import com.virtualmall.service.ShoppingCartService;

@Controller
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    //private Logger logger = Logger.getLogger(ShoppingCartController.class);

    @Autowired
    private ShoppingCartService shoppingCartService;
    
    @RequestMapping(value="/create",method=RequestMethod.POST)
    @ResponseBody
    public Map<String,String> create(ShoppingCart shoppingCart,HttpServletRequest request){
        //logger.info("create() method begin...");

        Map<String,String> result = new HashMap<String,String>();
        
        try {
            Account account = new Account();
            account.setId((Long)request.getSession().getAttribute(Constants.USER_ID_SESSION));
            shoppingCart.setAccount(account);
            
            List<ShoppingCart> sc = this.shoppingCartService.read(shoppingCart);
            
        	if(CollectionUtils.isEmpty(sc)){
            	shoppingCart.setCount(1L);
                shoppingCart.setCreateDate(new Date());
                
                this.shoppingCartService.create(shoppingCart);
        	}
            
            result.put("result", Constants.SUCCESS_RESULT_FLAG);
        } catch (Exception e) {
            //logger.error("create() method error...",e);
            
            result.put("result", Constants.FAILURE_RESULT_FLAG);
        }
        
        //logger.info("create() method end...");

        return result;
    }
    
    @RequestMapping(value="/list",method=RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> list(Page page,HttpServletRequest request){
        //logger.info("list() method begin...");

        Map<String,Object> result = new HashMap<String,Object>();
        
        try {
            Long userId = (Long)request.getSession().getAttribute(Constants.USER_ID_SESSION);
            
            List<ShoppingCart> shoppingCartList = this.shoppingCartService.getShoppingCartList(userId,page);

            for(ShoppingCart shoppingCart:shoppingCartList){
            	shoppingCart.getProduct().setAccount(null);
            }
            
            result.put("shoppingCartList", shoppingCartList);
            result.put("result", Constants.SUCCESS_RESULT_FLAG);
        } catch (Exception e) {
            //logger.error("list() method error...",e);
            
            result.put("result", Constants.FAILURE_RESULT_FLAG);
        }
        
        //logger.info("list() method end...");

        return result;
    }
    
    @RequestMapping(value="/delete",method=RequestMethod.POST)
    @ResponseBody
    public Map<String,String> delete(ShoppingCart shoppingCart){
        //logger.info("delete() method begin...");

        Map<String,String> result = new HashMap<String,String>();
        
        try {
            this.shoppingCartService.delete(shoppingCart);
            
            result.put("result", Constants.SUCCESS_RESULT_FLAG);
        } catch (Exception e) {
            //logger.error("delete() method error...",e);
            
            result.put("result", Constants.FAILURE_RESULT_FLAG);
        }
        
        //logger.info("delete() method end...");

        return result;
    }
    
    @RequestMapping(value="/update",method=RequestMethod.POST)
    @ResponseBody
    public Map<String,String> update(ShoppingCart shoppingCart){
        //logger.info("update() method begin...");

        Map<String,String> result = new HashMap<String,String>();
        
        try {
            ShoppingCart sc = this.shoppingCartService.read(shoppingCart.getId());
            
            sc.setCount(shoppingCart.getCount());
            
            this.shoppingCartService.update(sc);
            
            result.put("result", Constants.SUCCESS_RESULT_FLAG);
        } catch (Exception e) {
            //logger.error("update() method error...",e);
            
            result.put("result", Constants.FAILURE_RESULT_FLAG);
        }
        
        //logger.info("update() method end...");

        return result;
    }
    
}
